package org.learn.intermediate.com.exercise;

import java.util.Arrays;
import java.util.Comparator;

public class ArraryExercise {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book(40,"红楼梦");
        books[1] = new Book(70,"西游记后记");
        books[2] = new Book(60,"青年文摘");
        books[3] = new Book(50,"java从入门到放弃");
        //price 从大到小
        Arrays.sort(books, new Comparator() {
            //对book数组排序，这里的o1，o2就是Book对象；
            @Override
            public int compare(Object o1, Object o2) {
                Book book1 = (Book) o1;
                Book book2 = (Book) o2;
                return book1.getName().length()- book2.getName().length();
            //根据Book对象的name长度来从小到大排序
            }
        });
        System.out.println(Arrays.toString(books));
    }
}
class Book {
    private int price;
    private String name;
    //
    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Book(int price, String name) {
        this.price = price;
        this.name = name;

    }

    @Override
    public String toString() {
        return "Book{" +
                "price=" + price +
                ", name='" + name + '\'' +
                '}';
    }
}